-
-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[make:voter] generate type hints #853
Conversation
@@ -8,15 +8,15 @@ | |||
|
|||
class <?= $class_name ?> extends Voter | |||
{ | |||
protected function supports($attribute, $subject) | |||
protected function supports(<?= $use_type_hints ? 'string ' : null ?>$attribute, $subject): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have this conversation on every PR :p. Would generating the types break if I were using Symfony 4.4, where the parent class does NOT have those hints? If so, then we should do this - but only if we're on Symfony 5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - i keep forgetting how this works myself, but if the abstract does not declare a return type, the child is able to declare one. For arguments it would break iirc...
// https://www.php.net/manual/en/language.types.declarations.php
Note:
When overriding a parent method, the child's method must match any return type declaration on the parent. If the parent doesn't define a return type, then the child method may do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, then we should do this - but only if we're on Symfony 5.
The new $use_type_hints
var checks if we're using symfony 5 or greater (double check my comparison operator :D) for the arg. But this check isnt needed for the return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - i keep forgetting how this works myself, but if the abstract does not declare a return type, the child is able to declare one. For arguments it would break iirc...
Ah yes. It's legal to make a return type "more restrictive" than your parent, but it is NOT legal to make an argument type stricter (which would now mean you accept LESS input)
2bb10ff
to
95980dc
Compare
Thanks Jesse! |
starting in Symfony 5.0 the abstract voter methods introduced type hints (symfony/symfony@8c46b95). We now conditionally generate the
string
type hint for the$attribute
argument.Minor cleanup of the maker itself.